<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 开发响应式页面 -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录入口</title>
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.css">
    <style>
        #nav_div img{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            padding: 5px;
        }

        #login_div{
            /*border:1px solid #ccc;*/
            /*设置外边距*/
            margin-top: 50px;
            /*设置内填充*/
            padding-top: 30px;
            /*边框的阴影*/
            box-shadow: 2px -2px 4px #999;
            /*圆角*/
            border-radius: 10px;
        }
    </style>
</head>
<body>

<!--    导航begin -->
<div class="container-fluid" id="nav_div">
    <div class="row">
        <nav class="navbar navbar-inverse" role="navigation">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse"
                            data-target="#example-navbar-collapse">
                        <span class="sr-only">切换导航</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">大庆师范学院</a>
                </div>
                <div class="collapse navbar-collapse" id="example-navbar-collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="active"><a href="/views/user/index.html">首页</a></li>
                        <li><a href="#">admin</a></li>
                        <li><a href="#" data-toggle="modal" data-target="#myModal">登录</a></li>
                        <li><a href="#">注册</a></li>
                        <li><a href="/views/user/cart.html"><span class="glyphicon glyphicon-shopping-cart"></span>&nbsp;我的购物车</a></li>
                        <li><a href="#">我的订单</a></li>
                        <li><a href="#">安全退出</a></li>
                        <li><img src="/imgs/墨镜流泪.webp"></li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
</div>
<!--    导航end-->

<!--    登录模态框begin-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">用户登录入口</h4>
            </div>
            <div class="modal-body">
                <!--登录form表单begin-->
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="firstname" class="col-md-2 control-label">邮箱</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" id="firstname" placeholder="请输入邮箱">
                        </div>
                        <div class="col-md-2" style="margin-left: -30px">
                            <button type="button" class="btn btn-success">发送验证码</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="lastname" class="col-md-2 control-label">验证码</label>
                        <div class="col-md-9">
                            <input type="text" class="form-control" id="lastname" placeholder="请输入验证码">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox">请记住我
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <!--                                普通按钮-->
                            <button type="button" class="btn btn-default">登录</button>
                        </div>
                    </div>
                </form>
                <!--登录form表单end-->
            </div>
            <div class="modal-footer">

            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<!--    登录模态框end-->


<!--    登录表单-begin-->
<div class="container" id="login_div">
    <div class="row">
        <form class="form-horizontal" role="form">
            <div class="form-group">
                <label for="email" class="col-md-2 control-label">邮箱</label>
                <div class="col-md-7">
                    <input type="text" class="form-control" id="email" placeholder="请输入邮箱">
                    <span class="text-danger" id="email_error_span" style="display: none">email is not exits!</span>
                </div>
                <div class="col-md-2" style="margin-left: -30px">
                    <button type="button" class="btn btn-success" onclick="send_code()">发送验证码</button>
                </div>
            </div>
            <div class="form-group">
                <label for="lastname" class="col-md-2 control-label">验证码</label>
                <div class="col-md-8">
                    <input type="text" class="form-control" id="code" placeholder="请输入验证码">
                    <span class="text-danger" id="code_error_span" style="display: none">code is not right!</span>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox">请记住我
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-default" onclick="login()">登录</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!--    登录表单-end-->

<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/js/bootstrap.js"></script>
<script>
    //发送验证码
    function send_code(){
        //1. 获取邮箱对应的文本框的输入的数据
        let email = document.getElementById("email").value;
        //2. 发送get请求
        $.get(`/user/${email}`,function(res){
            //span标签
            let email_span = document.getElementById("email_error_span");
            if(res.code==404){
                //js操作css - 错误提示显示
                email_span.style.display="";
            }else if(res.code==200){
                email_span.style.display="none";
                alert("验证码发送成功!查收邮箱!")
            }
        })
    }

    //登录
    function login(){
        //jquery的写法来获取文本框输入的数据
        //js写法
        //let email = document.getElementById("email").value;

        //等同于上面的 jquery写法
        let email = $("#email").val();
        let code = $("#code").val();

        //发送post请求
        $.post(`/user/${email}/${code}`,function(res){
            let email_span = document.getElementById("email_error_span");
            let code_span = document.getElementById("code_error_span");
            if(res.code==404){
                //js操作css - 错误提示显示
                email_span.style.display="";
            }else if(res.code==500){
                code_span.style.display="";
            }else if(res.code==200){
                //console.log(token)
                //进行本地存储有两种方式,一种是cookie方式,一种是localStorage
                //key-value的形式
                localStorage.setItem("token", res.data);
                window.location='index.html';
            }
        })
    }
</script>
</body>
</html>